package com.guangjian.asc.retail.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.roo.addon.web.mvc.controller.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.guangjian.asc.common.web.BaseController;
import com.guangjian.asc.product.domain.Product;
import com.guangjian.asc.retail.domain.OrderDetail;
import com.guangjian.asc.retail.domain.SalesOrder;

@RooWebScaffold(path = "order-detail", formBackingObject = OrderDetail.class)
@RequestMapping("/order-detail")
@Controller
public class OrderDetailController extends BaseController {

	private static Logger log = Logger.getLogger(OrderDetailController.class);

	@RequestMapping(params = "form", method = RequestMethod.GET)
	public String createForm(Model model, @RequestParam(value = "supervisorId", required = true) Long supervisorId) {
		OrderDetail orderDetail = new OrderDetail();
		orderDetail.setSalesOrder(SalesOrder.findSalesOrder(supervisorId));

		model.addAttribute("orderDetail", orderDetail);
		List dependencies = new ArrayList();
		if (Product.countProducts() == 0) {
			dependencies.add(new String[] { "product", "products" });
		}
		if (SalesOrder.countSalesOrders() == 0) {
			dependencies.add(new String[] { "salesOrder", "salesorders" });
		}
		model.addAttribute("dependencies", dependencies);
		return "order-detail/create";
	}

	@RequestMapping(method = RequestMethod.POST)
	public String create(@Valid OrderDetail orderDetail, BindingResult result, Model model, HttpServletRequest request) {
		if (result.hasErrors()) {
			model.addAttribute("orderDetail", orderDetail);
			return "order-detail/create";
		}
		orderDetail.persist();
		return "redirect:/sales-order/" + encodeUrlSegment(orderDetail.getSalesOrder().getId().toString(), request);
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	public String delete(@PathVariable("id") Long id, HttpServletRequest request) {
		OrderDetail orderDetail = OrderDetail.findOrderDetail(id);
		String salesOrderId = orderDetail.getSalesOrder().getId().toString();
		orderDetail.remove();

		return "redirect:/sales-order/" + encodeUrlSegment(salesOrderId, request);
	}
    @RequestMapping(method = RequestMethod.PUT)
    public String update(@Valid OrderDetail orderDetail, BindingResult result, Model model, HttpServletRequest request) {
        if (result.hasErrors()) {
            model.addAttribute("orderDetail", orderDetail);
            return "order-detail/update";
        }
        orderDetail.merge();
        String salesOrderId = orderDetail.getSalesOrder().getId().toString();
        return "redirect:/sales-order/" + encodeUrlSegment(salesOrderId, request);
    }
}
